package com.sws.hmclient.crontab;

import com.sws.hmclient.dao.CpuinfoDao;
import com.sws.hmclient.dao.MeminfoDao;
import com.sws.hmclient.dao.SysfileinfoDao;
import com.sws.hmclient.dao.SysinfoDao;
import com.sws.hmclient.entity.Cpuinfo;
import com.sws.hmclient.entity.Meminfo;
import com.sws.hmclient.entity.Sysfileinfo;
import com.sws.hmclient.entity.Sysinfo;
import com.sws.hmclient.util.SystemInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.util.FormatUtil;

import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
public class main {


    @Autowired
    SysinfoDao sysinfodao;
    @Autowired
    CpuinfoDao cpudao;
    @Autowired
    MeminfoDao memdao;
    @Autowired
    SysfileinfoDao filedao;


    @Scheduled(cron = "0 0/5 * * * ? ")   //每1分钟执行一次
    public void SystemInfo() throws UnknownHostException, ParseException {
        SystemInfoUtils sysinfo = new SystemInfoUtils();


        //获取系统信息
        Sysinfo record = sysinfo.getSysInfo();
        String computerip = record.getComputerip();
        //获取CPU信息
        Cpuinfo cpuRecord = sysinfo.getCpuInfo();
        //获取内存信息
        Meminfo memRecord = sysinfo.getMemInfo();
        //获取盘符信息
        List<Sysfileinfo> FileRecordList = sysinfo.getSysFileInfo();

        //根据ip查询对应信息
        Sysinfo sys = sysinfodao.selectByIP(computerip);

        //操作系统信息表
        if (sys != null && !"".equals(sys)) {
            record.setUpdatetime(createTime());
            record.setCreatetime(sys.getCreatetime());
            record.setComputername(sys.getComputername());
            record.setIfsocket(1);
            sysinfodao.updateByPrimaryKey(record);
        } else {
            record.setCreatetime(createTime());
            sysinfodao.insert(record);
        }

        //操作cpu信息表
        cpuRecord.setComputerip(computerip);
        cpuRecord.setCreatetime(createTime());
        cpudao.insert(cpuRecord);

        //操作系统内存信息表
        memRecord.setComputerip(computerip);
        memRecord.setCreatetime(createTime());
        memdao.insert(memRecord);

        //操作系统盘符信息表
        int total = 0;
        int free = 0;
        int used = 0;
        for (Sysfileinfo File : FileRecordList) {
            float totali = Float.parseFloat(File.getTotal());
            float freei = Float.parseFloat(File.getFree());
            float usedi = Float.parseFloat(File.getUsed());
            total += totali;
            free += freei;
            used += usedi;
        }
        float d = (float) used / total;
        DecimalFormat dF = new DecimalFormat("0.00");
        Sysfileinfo fileinfo = new Sysfileinfo();
        fileinfo.setUsage(dF.format(d));
        fileinfo.setUsed(String.valueOf(used));
        fileinfo.setFree(String.valueOf(free));
        fileinfo.setTotal(String.valueOf(total));
        fileinfo.setComputerip(computerip);
        fileinfo.setCreatetime(createTime());
        filedao.insert(fileinfo);

    }


    //获取当前时间
    public static Date createTime() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStringParse = sdf.format(Calendar.getInstance().getTime());
        Date date = sdf.parse(dateStringParse);
        return date;
    }


    public static void main(String[] args) {


        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        printNetworkInterfaces(hal.getNetworkIFs());
    }

    private static void printNetworkInterfaces(NetworkIF[] networkIFs) {
        System.out.println("Network interfaces:");
        for (NetworkIF net : networkIFs) {

            boolean hasData = net.getBytesRecv() > 0 || net.getBytesSent() > 0 || net.getPacketsRecv() > 0
                    || net.getPacketsSent() > 0;
            System.out.format("   Traffic: received %s/%s%s; transmitted %s/%s%s %n",
                    hasData ? net.getPacketsRecv() + " packets" : "?",
                    hasData ? FormatUtil.formatBytes(net.getBytesRecv()) : "?",
                    hasData ? " (" + net.getInErrors() + " err)" : "",
                    hasData ? net.getPacketsSent() + " packets" : "?",
                    hasData ? FormatUtil.formatBytes(net.getBytesSent()) : "?",
                    hasData ? " (" + net.getOutErrors() + " err)" : "");
        }

    }
}
